import threading
import time
# sampleOne
'''
def Hi(name):
    print("Hello %s" %name)
    time.sleep(3)

if __name__=="__main__":
    t1 = threading.Thread(target=Hi,args=("萌萌",))
    t1.start()

    t2 = threading.Thread(target=Hi, args=("蒙蒙",))
    t2.start()
    # 画图（主、副进程）
    print("end")
'''
# sampleTwo
'''join:子线程完成之前主线程一直阻塞'''
'''
def music():
    print("begin to listen %s" %time.ctime())
    time.sleep(3)
    print("stop to listen %s" % time.ctime())
def game():
    print("begin to game %s" % time.ctime())
    time.sleep(5)
    print("stop to game %s" % time.ctime())

if __name__ == "__main__":
    t1 = threading.Thread(target=music, )
    t2 = threading.Thread(target=game, )
    t1.start()
    t2.start()
    # t1.join()
    t2.join()
    # 画图（1.主、副进程、添加end、2.t1添加join等待方法3.t2添加join等待方法、
    # 4.t1、t2同时添加等待方法、5.将添t2.join 移动到t2.start()之前则失去意义）

    print("over end ")
'''


# sampleThree
'''setDaemon:线程守护、子线程跟随主线程一起退'''
def music1():
    print("begin to listen %s" %time.ctime())
    time.sleep(3)
    print("stop to listen %s" % time.ctime())
def game1():
    print("begin to game %s" % time.ctime())
    time.sleep(5)
    print("stop to game %s" % time.ctime())

if __name__ == "__main__":
    t1 = threading.Thread(target=music1, )
    t2 = threading.Thread(target=game1, )
    t2.setDaemon(True)
    # 所有线程同时执行、主线程是否退取决于运行时间最长的线程是否为守护线程
    t1.start()
    t2.start()
    t1.setName("lessionMusic") # 设置线程名
    print("t1的线程名是：%s" % t1.getName())  # 获取线程名
    print(threading.enumerate())
    print("count:",threading.active_count()) # 获取线程数
# while threading.active_count()==1:
#     print("over end %s" %time.ctime())

# class Mythread(threading.Thread):
#     def __init__(self, num):
#         # threading.Thread.__init__(self)
#         super().__init__()
#         self.num = num
#     def run(self):
#         # music1()
#         # game1()
#         print("start num %d" % self.num)
# if __name__ == "__main__":
#     t1 = Mythread(1)
#     t2 = Mythread(2)
#     t1.start()
#     t2.start()
